package Reminder.view.pages;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.*;
import com.jfoenix.svg.SVGGlyph;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import Reminder.dao.RemindDao;
import Reminder.dao.impl.RemindDaoImpl;
import Reminder.domain.Message;
import Reminder.view.Controller;
import Reminder.view.GlobalConfig;
import Reminder.view.Main;
import Reminder.view.components.RemindCell;

import java.awt.*;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.util.Optional;

public class pageReminder extends Page {
    JFXListView<BorderPane> list = new JFXListView<>();
    StackPane container;
    BorderPane root = new BorderPane();
    public pageReminder(Stage stage, Controller controller) throws IOException, ParseException, AWTException {
        super(stage, controller);

        if(GlobalConfig.IS_LOGIN){
            JSONObject form = new JSONObject();
            form.put("userId",GlobalConfig.USER_ID);

        }
        else{
            System.out.println(getController().loadRemindList().toString());
            JSONObject message = JSONObject.parseObject(getController().loadRemindList().toString());
            if(message.getString("status" ) != "error"){
                GlobalConfig.ReminderList = JSONArray.parseArray(message.getString("data"));
                for(int i = 0;i < GlobalConfig.ReminderList.size();i++){
                    JSONObject item = JSONObject.parseObject(GlobalConfig.ReminderList.get(i).toString());
                    System.out.println(item.getString("remindTitle") + "," + item.getString("remindContent")+ "," +new Date(item.getLong("remindStartTime")));
                    if(item.getString("remindContent") == "null" || item.getString("remindContent") == "")
                        list.getItems().add(new RemindCell(i,item.getInteger("remindId"),item.getString("remindTitle"),"",new Date(item.getLong("remindStartTime")),getController(),getStage()));
                    else
                        list.getItems().add(new RemindCell(i,item.getInteger("remindId"),item.getString("remindTitle"),item.getString("remindContent"),new Date(item.getLong("remindStartTime")),getController(),getStage()));
                    try {
                        getController().saveRemindList(GlobalConfig.ReminderList);
                    } catch (IOException | ParseException e) {
                        e.printStackTrace();
                    }
                }
                container = new StackPane(list);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        root.setCenter(container);
                    }
                });
            }

        }


        System.out.println(GlobalConfig.USER_ID);

        JFXScrollPane pane = new JFXScrollPane();

        BorderPane buttonBar = new BorderPane();
        JFXButton backButton = new JFXButton("");
        SVGGlyph arrow = new SVGGlyph(0,
                "FULLSCREEN",
                "M402.746 877.254l-320-320c-24.994-24.992-24.994-65.516 0-90.51l320-320c24.994-24.992 65.516-24.992 90.51 0 24.994 24.994 "
                        + "24.994 65.516 0 90.51l-210.746 210.746h613.49c35.346 0 64 28.654 64 64s-28.654 64-64 64h-613.49l210.746 210.746c12.496 "
                        + "12.496 18.744 28.876 18.744 45.254s-6.248 32.758-18.744 45.254c-24.994 24.994-65.516 24.994-90.51 0z",
                Color.WHITE);

        arrow.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
//                            getStage().hide();
                        try {
                            pageOptions pageOptions = new pageOptions(getStage(),getController());
                        } catch (IOException | ParseException | AWTException e) {
                            e.printStackTrace();
                        }
//                            getStage().show();
                    }
                }
        );
        arrow.setSize(20, 16);
        backButton.setGraphic(arrow);
        backButton.setRipplerFill(Color.WHITE);
        buttonBar.setLeft(backButton);
        buttonBar.setPadding(new Insets(20));

        JFXButton addButton = new JFXButton("");
        SVGGlyph add = new SVGGlyph(0,
                "FULLSCREEN",
                "M874.0352 149.956267C777.335467 53.256533 648.7552 0 512 0 375.236267 0 246.664533 53.248 149.956267 149.956267 53.256533 246.664533 0 375.236267 0 512s53.248 265.335467 149.956267 362.043733C246.664533 970.743467 375.236267 1024 512 1024s265.335467-53.248 362.043733-149.956267C970.743467 777.335467 1024 648.763733 1024 512s-53.256533-265.335467-149.9648-362.043733zM554.666667 469.333333V315.682133A42.606933 42.606933 0 0 0 512 273.066667a42.666667 42.666667 0 0 0-42.666667 42.615466V469.333333H315.682133A42.606933 42.606933 0 0 0 273.066667 512a42.666667 42.666667 0 0 0 42.615466 42.666667H469.333333v153.6512A42.606933 42.606933 0 0 0 512 750.933333a42.666667 42.666667 0 0 0 42.666667-42.615466V554.666667h153.6512A42.606933 42.606933 0 0 0 750.933333 512a42.666667 42.666667 0 0 0-42.615466-42.666667H554.666667z m259.029333 344.362667C733.115733 894.293333 625.962667 938.666667 512 938.666667c-113.962667 0-221.115733-44.381867-301.696-124.970667C129.706667 733.1072 85.333333 625.962667 85.333333 512c0-113.962667 44.381867-221.115733 124.970667-301.696C290.8928 129.706667 398.037333 85.333333 512 85.333333c113.962667 0 221.115733 44.381867 301.696 124.970667C894.293333 290.884267 938.666667 398.037333 938.666667 512c0 113.9712-44.381867 221.115733-124.970667 301.696z",
                Color.WHITE);
        add.setSize(20);


        add.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            onAdd();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );

        addButton.setGraphic(add);
        addButton.setRipplerFill(Color.WHITE);
        buttonBar.setRight(addButton);
        addButton.getStyleClass().add("lalala");





        Label title = new Label("提醒事项");
        title.setPrefHeight(100);
        title.getStyleClass().add("title");
        title.setPadding(new Insets(0,0,0,20));
        JFXScrollPane.smoothScrolling((ScrollPane) pane.getChildren().get(0));

        buttonBar.setBottom(title);

        buttonBar.getStyleClass().add("buttonBar");



        StackPane.setMargin(title, new Insets(0, 0, 0, 40));
        StackPane.setAlignment(title, Pos.CENTER_LEFT);

        root.setTop(buttonBar);




        Scene scene = new Scene(root, GlobalConfig.WINDOW_CURRENT_WIDTH, GlobalConfig.WINDOW_CURRENT_HEIGHT, Color.WHITE);
        scene.getStylesheets().add(Main.class.getResource("/css/reminder.css").toExternalForm());
        stage.setScene(scene);
    }


    private void onAdd() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        JFXTextField remindTitle = new JFXTextField();
        remindTitle.setPromptText("请输入提醒标题");
        remindTitle.setPrefSize(100,20);
        content.getChildren().add(remindTitle);

        JFXTextField remindContent = new JFXTextField();
        remindContent.setPromptText("请输入提醒内容");
        remindContent.setPrefSize(100,20);
        content.getChildren().add( remindContent);

        FlowPane main = new FlowPane();
        main.setVgap(20);
        main.setHgap(20);

        JFXDatePicker datePickerFX = new JFXDatePicker();
        main.getChildren().add(datePickerFX);
        datePickerFX.setPromptText("请点击右侧图标选择日期");
        JFXTimePicker timePicker = new JFXTimePicker();
        timePicker.setPromptText("请点击右侧图标请选择时间");
        main.getChildren().add(timePicker);
        StackPane pane = new StackPane();
        pane.getChildren().add(main);
        content.getChildren().add(pane);

        

        BorderPane buttonBar = new BorderPane();

        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(200);
        dialogPane.setPrefHeight(100);



        Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                System.out.println("确定");
                if ( datePickerFX.getValue() == null || timePicker.getValue() == null  || remindTitle.getText().length() < 1){
                    getController().displayTray("输入非法","标题，提醒时间均不能为空","error");
                }
                else{
                    JSONObject form = new JSONObject();
                    form.put("userId",GlobalConfig.USER_ID);
                    form.put("remindTitle", remindTitle.getText());
                    form.put("remindContent",remindContent.getText());
                    form.put("remindStartTime",datePickerFX.getValue().toString() + " " + timePicker.getValue().toString());
                    form.put("remindId",-1);
                    GlobalConfig.ReminderList.add(form);
                    RemindDao remindDao = new RemindDaoImpl();
                    int userId = -1;
                    if(GlobalConfig.IS_LOGIN){
                        userId = GlobalConfig.USER_ID;
                    }
                    Message msg = remindDao.setRemindData(userId,GlobalConfig.ReminderList);
                    pageReminder pageReminder = new pageReminder(getStage(),getController());
                }
            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }


    }
}
